<template>
  <div class="inner-container">
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="addNotice">新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"/>
      <el-table-column label="序号" width="80">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * (queryParams.pageSize) + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="releaseTime" label="发布时间" min-width="100"/>
      <el-table-column prop="releaseTitle" label="公告标题" show-overflow-tooltip/>
      <el-table-column prop="publisher" label="发布人"/>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" link icon="View" @click="detail(scope.row.id)">详情</el-button>
          <el-button type="danger" link icon="Delete"  @click="del(scope.row.id)" v-if="$hasPermission('crm:system:notice:del')" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { onMounted, ref} from 'vue'
import request from '/src/utils/request'
import router from "@/router";
import {ElMessage} from "element-plus";

// --------------------------------------------------
const loading = ref(false)
const queryParams = ref({pageNum: 1, pageSize: 10})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])
const listData = () => {
  loading.value = true
  let data = {...queryParams.value}
  console.log(data);
  request.get('/api/crm/notice/select', {
    params: data
  }).then(res => {
    total.value = res.data.total;
    tableData.value = res.data.list;
    console.log(res.data)
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}
//查看详情
const detail = (id)=>{
  router.push('/system/notice/detail?id='+id);
}
//新增按钮方法
const addNotice = ()=>{
  router.push('/system/notice/add');
}
//删除方法
const del = (id)=>{
  request.post('/api/crm/notice/delete?id='+id)
      .then(res=>{
    ElMessage.success('删除成功')
    listData();
  })
}
// --------------------------------------------------
onMounted(() => {
   listData();
})
</script>

<style scoped>

</style>
